var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports", "core/Base.class", "core/Functions.module"], function (require, exports, Base, Functions) {
    "use strict";
    var BaseWidget = (function (_super) {
        __extends(BaseWidget, _super);
        function BaseWidget(settings) {
            var _this = _super.call(this) || this;
            _this.type = 'widget';
            _this.domObj = $();
            _this.started = false;
            _this.widgetPath ? null : _this.widgetPath = settings.widgetPath;
            _this.configPath = settings.configPath || undefined;
            _this.templatePath = settings.templatePath || undefined;
            _this.panelSelector = settings.panelSelector || undefined;
            _this.readyCallback = settings.readyCallback || undefined;
            _this.afterDestroyCallback = settings.afterDestroyCallback || undefined;
            _this.appConfig = _this.AppX.appConfig || {};
            _this.setStyle();
            _this.initConfig({
                configPath: _this.configPath
            }, function () {
                this.initTemplate({
                    templatePath: this.templatePath
                }, function () {
                    this._init();
                });
            });
            return _this;
        }
        BaseWidget.prototype._init = function () {
            this.startup();
            this.ready();
        };
        BaseWidget.prototype.startup = function () {
        };
        BaseWidget.prototype.ready = function () {
            var callback = this.readyCallback;
            this.readyCallback = null;
            if (callback) {
                callback();
            }
        };
        BaseWidget.prototype.destroy = function () {
            _super.prototype.destroy.call(this);
        };
        BaseWidget.prototype.afterDestroy = function () {
            if (this.afterDestroyCallback) {
                this.afterDestroyCallback();
            }
        };
        BaseWidget.prototype.initConfig = function (args, callback) {
            (function (that) {
                var url = Functions.concatUrl(that.widgetPath, args.configPath || "/config.json");
                $.ajax(url, {
                    dataType: "json",
                    success: function (data) {
                        that.config = data;
                        callback.call(that);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        that.bug("获取" + url + "失败:" + textStatus);
                    }
                });
            })(this);
        };
        BaseWidget.prototype.initTemplate = function (args, callback) {
            (function (that) {
                var url = Functions.concatUrl(that.widgetPath, args.templatePath || "/Widget.html");
                $.ajax(url, {
                    dataType: "text",
                    success: function (data) {
                        that.template = data;
                        callback.call(that);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        that.bug("获取" + url + "失败:" + textStatus);
                    }
                });
            })(this);
        };
        BaseWidget.prototype.setStyle = function (url) {
            var styleID = this.widgetPath.replace(/\//g, "-") + "-css";//var styleID = this.widgetPath.replace(/\//, "-") + "-css";
            if ($("#" + styleID).length > 0) {
                return;
            }
            var linkTag = $('<link id="' + styleID + '" rel="stylesheet" href="' + (url || this.widgetPath + "/css/style.css") + '"/>');
            $($('head')[0]).append(linkTag);
        };
        BaseWidget.prototype.setHtml = function (html, selector) {
            var currentSelector = selector || this.panelSelector || this.config.panelSelector || this.AppX.appConfig.mainContainer;
            if (currentSelector) {
                $(currentSelector).append(html);
            }
            else {
                $($('body')[0]).append(html);
            }
            this.setDomObj();
        };
        BaseWidget.prototype.setDomObj = function () {
            if (this.baseClass === undefined) {
            }
            else {
                if (this.baseClass !== "") {
                    this.domObj = $('.' + this.baseClass);
                }
            }
        };
        return BaseWidget;
    }(Base));
    return BaseWidget;
});
